<template>
  <div>
    <el-table :data="items">
      <el-table-column type="index" width="50" label="序号"></el-table-column>
      <el-table-column prop="_id" label="数据库id" width="230"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            id='btn-edit'
            size="mini"
            icon="el-icon-edit"
            type="primary"
            @click="$router.push(`/admin_users/edit/${scope.row._id}`)">编辑</el-button>
          <el-button
            size="mini"
            icon="el-icon-delete"
            type="danger"
            @click="remove(scope.row)" style="margin:5px 0 0 0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: []
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    async fetch() {
      const response = await this.$http.get("rest/admin_users");
      this.items = response.data;
    },
    async remove(row){

      this.$confirm(`是否删除?"${row.name}" `, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        .then( async () => {
          await this.$http.delete(`rest/admin_users/${row._id}`)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });

          this.fetch();
        })
    }
  }
};
</script>

<style scoped>
#btn-edit{
  margin-right: 10px!important;
}
</style>